const path = require('path')
const webpackConfig = require('./webpack.config.js')
const { merge }= require('webpack-merge')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = merge(webpackConfig, {
  mode: 'production',
  devtool: 'cheap-module-source-map',
  devServer:{
    port: 8080,
    contentBase: path.join(__dirname, 'dist')
  },
  plugins: [
    new CopyWebpackPlugin({
      patterns: [{
          from: path.resolve(__dirname,'./public'),
          to: path.resolve(__dirname,'./dist')
        }]
    }),
  ],
  optimization: { // 优化配置 
    minimizer: [ // 告知 webpack 使用 TerserPlugin 或其它在 optimization.minimizer 定义的插件压缩 bundle
      new UglifyJsPlugin({ // 压缩js
        cache: true,
        parallel: true,
        sourceMap: true
    }),
    new OptimizeCssAssetsPlugin({}) // 压缩 css
    ],
    splitChunks: { // 分块策略
      chunks: 'all', // 哪些 chunk 被优化 all/async/initial
      cacheGroups: {
        libs: {
          name: "chunk-libs",
          test: /[\\/]node_modules[\\/]/,
          priority: 10,
          chunks: "initial" // 只打包初始时依赖的第三方
        }
      }
    }
  }
})

